package cn.work.control;

import cn.work.dao.SearchInReservationDao;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "getAllServiceServlet", value = "/getAllServiceServlet")
public class getAllServiceServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 获取所有服务项目
            SearchInReservationDao serviceDao = new SearchInReservationDao();
            List<Map<String, Object>> serviceList = serviceDao.getAllServices();
            // 使用 Gson 将服务项目列表转换为 JSON
            Gson gson = new Gson();
            String json = gson.toJson(serviceList);

            // 设置响应类型为 JSON
            response.setContentType("text/html;charset=UTF-8");
            response.setContentType("application/json");
            response.getWriter().write(json);
        } catch (SQLException e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write("Database error: " + e.getMessage());

        }
    }
}
